package com.example.websocket;

import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.io.watch.WatchMonitor;
import com.xiaoleilu.hutool.io.watch.Watcher;
import com.xiaoleilu.hutool.lang.Console;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.WatchEvent;
import java.sql.Date;
import java.text.DateFormat;

/**
 * Created by dw on 2018/01/05
 */
public class LogFile {
    public static void main(String[] args) {
        addMessage();
    }

    public static void addMessage() {
        File file = FileUtil.file("C:\\projects_openproject\\boot-websocket-log\\log\\spring.log");
        System.out.println(file.getAbsolutePath());
        WatchMonitor watchMonitor = WatchMonitor.create(file, WatchMonitor.ENTRY_MODIFY);
        watchMonitor.setWatcher(new

                                        Watcher() {
                                            @Override
                                            public void onCreate(WatchEvent<?> event, Path currentPath) {
                                                Object obj = event.context();
                                                Console.log("创建：{}-> {}", currentPath, obj);
                                            }

                                            @Override
                                            public void onModify(WatchEvent<?> event, Path currentPath) {
                                                LoggerMessage loggerMessage = new LoggerMessage(
                                                        "修改：{}-> {}"
                                                        , null,
                                                        null,
                                                        null,
                                                        "debugg"
                                                );
                                                LoggerQueue.getInstance().push(loggerMessage);

                                                Object obj = event.context();
                                                Console.log("修改：{}-> {}", currentPath, obj);
                                            }

                                            @Override
                                            public void onDelete(WatchEvent<?> event, Path currentPath) {
                                                Object obj = event.context();
                                                Console.log("删除：{}-> {}", currentPath, obj);
                                            }

                                            @Override
                                            public void onOverflow(WatchEvent<?> event, Path currentPath) {
                                                Object obj = event.context();
                                                Console.log("Overflow：{}-> {}", currentPath, obj);
                                            }
                                        });

        //设置监听目录的最大深入，目录层级大于制定层级的变更将不被监听，默认只监听当前层级目录
        watchMonitor.setMaxDepth(3);
        //启动监听
        watchMonitor.start();
    }
}
